#导入相应的模块
import pygame
from pygame.locals import *
import time,random

def key_control(hero_temp):
	'''键盘控制函数'''
	#执行退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			print("exit()")
			exit()

	#获取按键信息
	pressed_keys = pygame.key.get_pressed()
	#print(pressed_keys)
	#做判断，并执行对象的操作
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print("Left...")
		hero_temp.move_left()

	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print("Right...")
		hero_temp.move_right()

	if pressed_keys[K_SPACE]:
		print("space...")
		hero_temp.fire()

class Bullet:
	'''子弹类'''
	def __init__(self, screen_temp, x, y):
		self.x = x + 53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/pd.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image, (self.x, self.y))

	def move(self):
		self.y -= 10
		if self.y <= -20:
			return True

	# 敌机子弹每次向下移动8个像素，
	def move_enemy(self):
		self.y += 8
		if self.y >= 568:
			return True
		
class Boom:
	'''
	爆炸效果类
	'''
	def __init__(self,screen_temp,x,y):
		self.x = x
		self.y = y
		self.screen = screen_temp
		#self.image = pygame.image.load("./images/bm"+str(random.choice(range(11)))+".png")
		self.bomb_picture_list = [] #用来存储爆炸时需要的图片
		self.picture_num=10
		self.i=0
		while self.i<self.picture_num:
			print("self.i[{}]".format(self.i))
			self.image = pygame.image.load("./images/boom"+str(self.i)+".png");
			self.bomb_picture_list.append(self.image)
			self.i+=1
			

	def display(self):
		self.image_index=0
		while True:
			self.screen.blit(self.bomb_picture_list[self.image_index], (self.x, self.y))
			self.image_index+=1
			if self.image_index==self.picture_num:
				break
        
class HeroPlane(Boom):
	'''玩家飞机（英雄）'''
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 400
		self.gamover=False
		self.hit = False
		self.screen = screen_temp
		self.image = pygame.image.load("./images/me.png")
		self.bullet_list = [] #用于存放玩家的子弹列表


	def display(self):
		'''绘制玩家飞机'''
		if self.hit==False:
			#绘制子弹
			for b in self.bullet_list:
				b.display()
				if b.move():
					self.bullet_list.remove(b)
			self.screen.blit(self.image,(self.x,self.y))
		else:
			Boom.__init__(self, self.screen, self.x, self.y)
			Boom.display(self)
		
	def sethit(self):
		self.hit=True

	def setgameover(self):
		self.gamover=True

	def getgameover(self):
		return  self.gamover

	def move_left(self):
		'''左移动飞机'''
		self.x -= 5
		if self.x <= 0:
			self.x=0

	def move_right(self):
		'''右移动飞机'''
		self.x += 5
		if self.x>=406:
			self.x=406
	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))
		print(len(self.bullet_list))
	
	#玩家飞机与敌机子弹和敌机做碰撞检测
	def crash_check(self,enemy):
		#玩家飞机与敌机子弹
		for bo in enemy.bullet_list_enemy:
			if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60:
				enemy.bullet_list_enemy.remove(bo)
				return 1
		#玩家飞机与敌机
		if enemy.x>=self.x and enemy.x<=self.x+106 and enemy.y>=self.y+20 and enemy.y<=self.y+60:
				return 2
		if enemy.x+88>=self.x and enemy.x+88<=self.x+106 and enemy.y>=self.y+20 and enemy.y<=self.y+60:
				return 2
		if enemy.x>=self.x and enemy.x<=self.x+106 and enemy.y+64>=self.y+20 and enemy.y+64<=self.y+60:
				return 2
		if enemy.x+88>=self.x and enemy.x+88<=self.x+106 and enemy.y+64>=self.y+20 and enemy.y+64<=self.y+60:
				return 2
			
class EnemyPlane(Boom):
	'''敌机类'''
	def __init__(self,screen_temp):
		self.x = random.choice(range(408))
		self.y = -75
		self.hit = False
		self.screen = screen_temp
		self.image = pygame.image.load("./images/e"+str(random.choice(range(3)))+".png")
		self.bullet_list_enemy = [] #用于存放敌机的子弹列表


	def display(self):
		if self.hit==False:
			'''绘制敌机'''
			self.screen.blit(self.image,(self.x,self.y))
			#绘制子弹
			for b in self.bullet_list_enemy:
				b.display()
				if b.move_enemy():
					self.bullet_list_enemy.remove(b)
			self.screen.blit(self.image,(self.x,self.y))
		else:
			Boom.__init__(self, self.screen, self.x, self.y)
			Boom.display(self)

	def sethit(self):
		self.hit=True

	def move(self):
		self.y += 4
		#敌机出屏幕
		if self.y>568:
			return True

	def crash_check(self,hero):
		# 遍历所有子弹,并执行碰撞检测
		for bo in hero.bullet_list:
			if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60:
				hero.bullet_list.remove(bo)
				return True
				
	#敌机发射子弹
	def fire(self):
		#随机发射子弹
		if random.choice(range(20))==10:
			self.bullet_list_enemy.append(Bullet(self.screen,self.x,self.y))
			print("enemy:%d"%len(self.bullet_list_enemy))
			return True

def main():
	'''主程序函数 '''
	# 创建游戏窗口，
	screen = pygame.display.set_mode((512,568),0,0)

	# 创建一个游戏背景
	background = pygame.image.load("./images/bg2.jpg")

	# 创建玩家飞机（英雄）
	hero = HeroPlane(screen)

	m = -968
	enemylist = [] #存放敌机的列表
	while True:
		#绘制画面
		screen.blit(background,(0,m))
		m+=2
		if m>=-200:
			m = -968

		#绘制玩家飞机
		hero.display()
		
		#执行键盘控制
		key_control(hero)

		#随机绘制敌机
		if random.choice(range(50))==10:
			enemylist.append(EnemyPlane(screen))
		#遍历敌机并绘制移动
		for em in enemylist:
			em.display()
			em.move()
			em.fire()
			if em.crash_check(hero):
				em.sethit()
				em.display()
				enemylist.remove(em)

			#玩家死了
			if hero.crash_check(em)==1:
				hero.sethit()
				hero.setgameover()
				break

			#玩家与敌机都死了
			if hero.crash_check(em)==2:
				hero.sethit()
				em.display()
				enemylist.remove(em)
				hero.setgameover()
				break

		
		#游戏结束
		if hero.getgameover():
			break
		#更新显示
		pygame.display.update()

		#定时显示
		time.sleep(0.04)


#判断当前是否是主运行程序，并调用
if __name__ == "__main__":
	main()
